package com.kqb.controller;

import com.kqb.common.controller.base.BaseController;
import com.kqb.common.domain.AjaxResult;
import com.kqb.common.domain.ErrorResult;
import com.kqb.common.domain.page.PageResult;
import com.kqb.common.group.ApiGroups;
import com.kqb.pojo.Customer;
import com.kqb.pojo.dto.CustomerDTO;
import com.kqb.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags = "客户管理")
public class CustomerController extends BaseController {

    @Autowired
    CustomerService customerService;


    /**
     * 添加用户
     *
     * @param customerDTO
     * @param bindingResult
     * @return
     */
    @PostMapping("/customer")
    @ApiOperation("添加客户信息") // OK 完成
    public AjaxResult save(@Validated(ApiGroups.Save.class) @RequestBody CustomerDTO customerDTO, BindingResult bindingResult) {
        //数据校验
        ErrorResult.validate(bindingResult);
        //校验完成后判断前端对应的规则--如账户密码
        String account =customerDTO.getAccount();
        String password = customerDTO.getPassword();
        if(account.length()<=12 && account.length()>=6 && password.length()>=6 && password.length()<=12 ){
            //进行添加操作--具体实行服务层实现
            Map<String, Object> map = customerService.save(customerDTO);
            int save = (int) map.get("code");
            String mes = map.get("mes").toString();
            if (save == 1) {
                return success(mes);
            } else {
                return error(mes);
            }
        }
       return error("账号或密码格式有误!");
    }


    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/customer")
    @ApiOperation("删除客户信息") // OK 完成
    public AjaxResult delete(int id) {
        int delete = customerService.delete(id);
        if (delete == 1) {
            return success("删除成功");
        } else {
            return error("该用户不存在,删除失败");
        }
    }


    /**
     * 修改客户
     *
     * @param customerDTO
     * @return
     */
    @PutMapping("/customer")
    @ApiOperation("修改客户信息")  //  OK 完成
    public AjaxResult update(@Validated(ApiGroups.Update.class) @RequestBody CustomerDTO customerDTO, BindingResult bindingResult) {
        //数据校验,修改必需传递id
        ErrorResult.validate(bindingResult);
        //进行客户修改
        int update = customerService.update(customerDTO);
        if (update == 1) {
            return success("修改用户成功");
        } else {
            return error("修改对象失败");
        }
    }


    /**
     * 查询客户
     *
     * @param customer
     * @return
     */
    @ApiOperation("查询客户信息")//customer
    @PostMapping("/customer/list") // OK 完成
    public AjaxResult query(@RequestBody Customer customer) {
        PageResult query = customerService.query(customer);
        return success(query);
    }

    @ApiOperation("根据id查询所有信息")
    @GetMapping("/customer")
    public AjaxResult queryById(int id){
        Map<String, Object> map = customerService.queryById(id);
        return success("查询成功",map);
    }

    @ApiOperation("检查账号唯一性")
    @GetMapping("/customer/chk")
    public AjaxResult accountIsExist(String account){
        int i = customerService.accountIsExist(account);
        System.out.println(account);
        if(i!=0){
            return error("账号已存在!");
        }
       return success("账号可以使用!");
    }


    @ApiOperation("查询简单的客户信息")
    @GetMapping("/customer/simple")
    public AjaxResult selectSimple(){
        return customerService.selectSimple();
    }


}
